package com.gamex.findit.controller.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.gamex.findit.model.Member;
import com.gamex.findit.model.Notice;
import com.gamex.findit.service.MemberService;

@Controller("adminMember")
public class MemberController {
	
	private static final Logger logger = LoggerFactory.getLogger(MemberController.class);
	
	@Autowired MemberService memberService;
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param member
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/memberList")
	public String memberList( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, Model model
							, @ModelAttribute Member member) throws Exception{
	
		int count = memberService.getMemberCount(member);
		List<Member> result = memberService.getMember(member);
		
		model.addAttribute("list", result);
		model.addAttribute("count", count);
		model.addAttribute("pageNo", member.getPageNo());
		model.addAttribute("pageGroup", member.getPageGroup());
		model.addAttribute("searchParam", member);
	return "/member/memberList";
	}

	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param member
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/viewMember")
	public String viewMember( HttpSession session
							, HttpServletRequest request
							, HttpServletResponse response
							, ModelMap model
							, @ModelAttribute Member member) throws Exception{
		
		model.addAttribute("view", memberService.getMemberView(member.getUserId()));
		model.addAttribute("searchParam", member);
	return "/member/memberView";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param member
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/addMember")
	public String addMember( HttpSession session
						   , HttpServletRequest request
						   , HttpServletResponse response
						   , Model model
						   , @ModelAttribute Member member) throws Exception{
		
		memberService.insertMember(member);
		
	return "redirect:/admin/memberList.json";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param member
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/updateMember")
	public String updateMember( HttpSession session
							  , HttpServletRequest request
							  , HttpServletResponse response
							  , Model model
							  , @ModelAttribute Member member) throws Exception{
		
		memberService.updateMember(member);
		
	return "redirect:/admin/memberList.json";
	}
	
	/**
	 * @param session
	 * @param request
	 * @param response
	 * @param model
	 * @param member
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/admin/deleteMember")
	public String deleteMember( HttpSession session
							  , HttpServletRequest request
							  , HttpServletResponse response
							  , Model model
							  , @ModelAttribute Member member) throws Exception{
		
		
		memberService.deleteMember(member);
		
	return "redirect:/admin/memberList.json";
	}
}
